package com.sjj.shopping.controller;

import com.sjj.shopping.pojo.Message;
import com.sjj.shopping.webSocket.WebSocketServer;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.ArrayList;

/**
 * className:OnlineChat
 * Package:com.sjj.shopping.controller
 * Description:
 *
 * @Date: 2022/8/14 20:21
 * @Auther: wssky19890629@163.com
 */

@Controller
public class OnlineChat {

    @RequestMapping("/chat/{hisUserId}")
    public ModelAndView socket(@PathVariable String hisUserId, HttpServletRequest request){
        //获取登录用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        String myUserId = user.getUserId();

        ModelAndView modelAndView = new ModelAndView("admin/adminChat.html");
        //websocket地址
        modelAndView.addObject("bathPath",request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/");
        modelAndView.addObject("hisUserId",hisUserId);
        modelAndView.addObject("myUserId",myUserId);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/push/{myUserId}/{hisUserId}/{msg}")
    public String push(@PathVariable String myUserId,@PathVariable String hisUserId,@PathVariable String msg){
        System.out.println(msg+"  "+myUserId+"  "+hisUserId);
        WebSocketServer.sendInfo(myUserId,hisUserId,msg);
        return "ok";
    }
}
